class Solution {
    public static void main(String[] args) {
        String s="aaabb";
        int k=3;
        longestSubstring(s, k);
    }
    public static int longestSubstring(String s, int k) {
        if (k == 1) {
            return s.length();
        }
        if (k > s.length()) {
            return 0;
        }
        int res=0;

        //进行循环
        for(int i=1;i<=26;++i){

            res=Math.max(res,window(s,k,i));
        }
        return res;
    }
    public static int window(String s,int k,int kind){
        //创建哈希表
        int[]hash=new int[26];
        //创建双指针
        int left=0;
        int right=0;
        //定义max
        int maxLength=0;
        //定义category
        int category=0;
        int satisty=0;
        while(right<s.length()){
            hash[s.charAt(right)-'a']++;
            if(hash[s.charAt(right)-'a']==1);
            category++;
            if(hash[s.charAt(right)-'a']==k){
                satisty++;
            }
            while(category>kind){
                hash[s.charAt(left)-'a']--;
                if(hash[s.charAt(left)-'a']==0){
                    category--;
                }
                if(hash[s.charAt(left)-'a']==k-1){
                    satisty--;
                }
                left++;
            }
            if(category==satisty){
                if(maxLength<(right-left+1)){
                    maxLength=right-left+1;
                }
            }
            right++;
        }
        return maxLength;

    }
}